package com.threeonemeeting.activity;

import java.util.Map;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.MeetingCommentInfoListAdapter;
import com.threeonemeeting.list.MeetingCommentInfoList;
import com.threeonemeeting.listview.DragRefreshListView;
import com.threeonemeeting.model.MeetingCommentInfo;
import com.threeonemeeting.model.MeetingCommentListItemInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.task.GetMeetingCommentsListTask;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.util.MiscUtil;

public class MeetingCommentsListActivity extends BaseTabActivity implements
		OnItemClickListener, ICallBack {

	private int nMeetingId = 0;
	protected boolean bForceRefresh = false;

	protected LinearLayout loadingMoreLayout = null;

	private MeetingCommentInfoList listComments = null;
	private MeetingCommentInfoList listRootComments = null;
	private MeetingCommentInfoListAdapter mAdapter;

	private DragRefreshListView listViewComments;

	/**
	 * @desc 显示加载更多的状态窗口
	 * @param bShow
	 */
	protected void showLoadingMoreLayout(boolean bShow) {

		if (bShow == true) {
			loadingMoreLayout.setVisibility(View.VISIBLE);

		} else {
			loadingMoreLayout.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_meeting_comment_list);
		MiscUtil.setBackBtnListener(this);
		nMeetingId = getIntent().getExtras().getInt("id");
		bForceRefresh = getIntent().getExtras().getBoolean("refresh");

		MiscUtil.showLoadingLayout(this, "正在加载评论列表");

		// ListView
		loadingMoreLayout = (LinearLayout) findViewById(R.id.layout_loading_more);

		mAdapter = new MeetingCommentInfoListAdapter(this);
		listViewComments = (DragRefreshListView) findViewById(R.id.lv_meeting_comments);
		listViewComments.setAdapter(mAdapter);
		listViewComments.setOnItemClickListener(this);
		listViewComments.setCacheColorHint(Color.TRANSPARENT);
		listViewComments
				.setOnLoadMoreListener(new DragRefreshListView.OnLoadMoreListener() {

					public void onLoadMore(int nNextPage) {
						getNextPage(nNextPage);

						showLoadingMoreLayout(true);
					}
				});

		// 添加评论的按钮
		Button btnAddComment = (Button) findViewById(R.id.btn_addcomment);
		btnAddComment.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				// 检查用户是否已经登录，如果没有登录则首先跳转到LoginActivity
				if (DataCenter.getInstance().currentLoginUserInfo.nUserId == 0) {
					MessageBox.showToast(MeetingCommentsListActivity.this,
							"需要登录后才能评论，请先登录");

					Bundle b = new Bundle();
					b.putString("jumpto", "SendCommentActivity");

					Bundle bundle = new Bundle();
					bundle.putInt("eid", nMeetingId);
					bundle.putInt("tid", 0);
					b.putBundle("bundle", bundle);
					startActivity("LoginActivity", b);
				} else {
					Bundle b = new Bundle();
					b.putInt("eid", nMeetingId);
					b.putInt("tid", 0);
					startActivity("SendCommentActivity", b);
				}
			}
		});

		// 请求评论列表
		GetMeetingCommentsListTask task = new GetMeetingCommentsListTask(
				MeetingCommentsListActivity.this,
				MeetingCommentsListActivity.this);
		task.setPages(1);

		if (bForceRefresh == true) {
			task.setRefresh(true);
		}

		Bundle params = new Bundle();
		params.putString("eid", String.valueOf(nMeetingId));
		params.putString(
				"uid",
				String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
		params.putString("pass",
				DataCenter.getInstance().currentLoginUserInfo.strPassword);
		params.putString("page", String.valueOf(1));
		TaskManager.addTask(task, params, true);
	}

	/**
	 * @desc 请求返回下一页的列表内容 子类重载该函数
	 * @param nPage
	 */
	protected void getNextPage(int nPage) {

		// 请求评论列表
		GetMeetingCommentsListTask task = new GetMeetingCommentsListTask(
				MeetingCommentsListActivity.this,
				MeetingCommentsListActivity.this);
		task.setCached(false);
		task.setPages(nPage);
		Bundle params = new Bundle();
		params.putString("eid", String.valueOf(nMeetingId));
		params.putString(
				"uid",
				String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
		params.putString("pass",
				DataCenter.getInstance().currentLoginUserInfo.strPassword);
		params.putString("page", String.valueOf(nPage));
		TaskManager.addTask(task, params, true);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		int nIndex = arg2 - 1;
		if (nIndex < 0)
			return;

		if (nIndex > mAdapter.getCount() - 1)
			return;

		MeetingCommentListItemInfo lvInfo = mAdapter.getItem(nIndex);
		if (lvInfo == null)
			return;

		int nCommentId = lvInfo.nCommentId;
		MeetingCommentInfo mInfo = listComments.getCommentInfo(nCommentId);
		if (mInfo == null)
			return;

		MeetingCommentInfo repliedInfo = listComments
				.getCommentInfo(mInfo.nCommentId);

		Bundle b = new Bundle();
		b.putSerializable("comment", mInfo);
		b.putSerializable("rcomment", repliedInfo);
		startActivity("MeetingCommentDetailActivity", b);
	}

	public void doCallBack(Map<String, Object> mapData) {
		Integer msgType = (Integer) mapData.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		if (msgType != HttpConst.TASKTYPE_MEETINGCOMMENTS)
			return;

		listViewComments.stopLoadingMore();

		listComments = (MeetingCommentInfoList) mapData
				.get(Const.Keys.KEY_LIST);
		if (listComments == null) {
			return;
		}

		listRootComments = (MeetingCommentInfoList) mapData.get("root");

		Integer nPage = (Integer) mapData.get("page");
		if (nPage == null) {
			nPage = 1;
		}

		if (nPage == 1) {
			mAdapter.clear();
			MiscUtil.showListLayout(this, listComments.size());
		}

		if (listComments.size() == 0 || listComments.size() < Const.PAGE_NUM) {
			listViewComments.setLastPage(nPage);
		}

		for (int i = 0; i < listComments.size(); i++) {
			MeetingCommentInfo info = listComments.get(i);
			if (info == null)
				continue;

			MeetingCommentListItemInfo lvInfo = new MeetingCommentListItemInfo();
			lvInfo.nCommentUserId = info.nCommentUserId;
			lvInfo.nMeetingId = info.nMeetingId;
			lvInfo.nCommentId = info.nCommentId;
			lvInfo.nReplyNum = info.nReplyNum;
			lvInfo.strCommentName = info.strCommentName;
			lvInfo.strCommentTime = info.strCommentTime;
			lvInfo.strCommentText = info.strCommentText;
			lvInfo.bIsHaveImage = info.bIsHaveImage;
			lvInfo.bReplied = info.bReplied;

			if (listRootComments != null) {
				MeetingCommentInfo repliedInfo = listRootComments
						.getCommentInfo(info.nRootId);
				if (repliedInfo != null) {
					lvInfo.strRepliedCommentName = repliedInfo.strCommentName;
					lvInfo.strRepliedCommentText = repliedInfo.strCommentText;
					lvInfo.strRepliedCommentTime = repliedInfo.strRepliedCommentTime;
				}
			}

			mAdapter.add(lvInfo);
		}

		mAdapter.notifyDataSetChanged();

		listViewComments.onLoadMoreComplete();
		showLoadingMoreLayout(false);
	}
}
